package com.wn.utils;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yellow Docter
 * @date 2024-07-09
 * @desc 蜗牛创想：www.woniuxy.com
 */
public class JwtUtils {

    public static String getTokenToUsername(){

        //00 获取头信息 request中获取 RequestContextHolder
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        //01 拿到token
        String token = request.getHeader("Token");
        //02 校验解析
        if (JWTUtil.verify(token,"1234".getBytes())) {
            JWT jwt = JWTUtil.parseToken(token);
            return jwt.getPayload("username").toString();
        }
        return null;
    }

}
